package com.xd.study.utility;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by DGF on 2015/4/28.
 * 自定义的PageAdaper，用于在ViewPager中添加View；
 * 三个一组进行加载，其他的会自动销毁；
 */
public class MyPagerAdapter extends PagerAdapter {

    private List<View> viewList;

    private List<String> titleList;

    public MyPagerAdapter(List<View> views, List<String> titles){
        this.viewList = views;
        this.titleList=titles;
    }

    /**
     * 返回页卡的数量
     * @return
     */
    @Override
    public int getCount() {
        return this.viewList.size();
    }

    /**
     * View是否来自于这个对象
     * @param view
     * @param o
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view==o;
    }

    /**
     * 实例化一个页卡
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //return super.instantiateItem(container, position);

        container.addView(this.viewList.get(position));
        return this.viewList.get(position);
    }

    /**
     * 销毁一个页卡
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);

        container.removeView(this.viewList.get(position));
    }

    /**
     * 获取标题
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return this.titleList.get(position);
    }

}
